using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entites;
namespace DAL
{
   public class KhachHangDAL
    {
         private SqlConnection cn = null;
        public KhachHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (KhachHang kh)
        {
            SqlCommand cm = new SqlCommand("KhachHang_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenKH",kh.tenkh);
           cm.Parameters.AddWithValue("@Email",kh.email);
           cm.Parameters.AddWithValue("@DienThoai",kh.dienthoai);
           cm.Parameters.AddWithValue("@TaiKhoan",kh.taikhoan);
           cm.Parameters.AddWithValue("@MatKhau",kh.matkhau);
          


            
           
            cm.ExecuteNonQuery();
        }
        public void Update(KhachHang kh)
        {
            SqlCommand cm = new SqlCommand("KhachHang_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaKH", kh.makh);
            cm.Parameters.AddWithValue("@TenKH",kh.tenkh);
           cm.Parameters.AddWithValue("@Email",kh.email);
           cm.Parameters.AddWithValue("@DienThoai",kh.dienthoai);
           cm.Parameters.AddWithValue("@TaiKhoan",kh.taikhoan);
           cm.Parameters.AddWithValue("@MatKhau",kh.matkhau);
          
          
          
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("KhachHang_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaKH", iD);
            cm.ExecuteNonQuery();
        }

        public tbKhachHangCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("KhachHang_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbKhachHangCollection khCollection = new tbKhachHangCollection();

            while (dr.Read())
            {
               KhachHang khachhang = new KhachHang();
                khachhang.makh = int.Parse(dr.GetValue(0).ToString());
                khachhang.tenkh = dr.GetValue(1).ToString();
                khachhang.email = dr.GetValue(2).ToString();
                khachhang.dienthoai = dr.GetValue(3).ToString();
                khachhang.taikhoan = dr.GetValue(4).ToString();
                khachhang.matkhau = dr.GetValue(5).ToString();


                khCollection.Add(khachhang);


             
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return khCollection;
        }
    }
    }

